package it.magazzinoCofi.view;

import it.magazzinoCofi.annotation.Singleton;
import it.magazzinoCofi.controllers.AbstractController;
import it.magazzinoCofi.controllers.UserLoginController;
import it.magazzinoCofi.model.DataUserModel;

import java.util.Observable;
import java.util.Observer;

import Data.ErrorType;
import Data.Notify;
import Data.Utente;

public class PView implements Observer {
	@Singleton
	private static PView Main;

	BaseFrame JMain;
	AbstractController controller;

	// private DataUserModel m_model;

	public PView(BaseFrame Frame) {
		Main = this;

		/**
		 * SETUP iniziale Login
		 */
		UserLoginFrame login = new UserLoginFrame();
		DataUserModel model = new DataUserModel();
		UserLoginController controller = new UserLoginController(model, login);
		login.setVisible(true);

		JMain = login;
		// m_model = model;

	}

	public BaseFrame getFrame() {
		return JMain;
	}

	@Override
	public void update(Observable o, Object arg) {
		System.out.println("CHANGED");
		switch (((Notify) arg).getErrorType()) {
		case CHANGE_PAGE:
			/**
			 * case CHANGE_PAGE
			 * Viene sovrascritto il frame, ad esempio durante la fase di login 
			 */
			JMain.displayErrorMessage(Utente.getInstance().getUser());
			
			JMain.setVisible(false);
			
			/**
			 * Ricevo il BaseFrame e lo gestisco.
			 */
			//JMain=((Notify) arg).getFrame();
			controller=((Notify) arg).getController();
			JMain=controller.getM_view();
			//JMain = new SchermataPrincipale(Utente.getInstance().getUser());
			
			System.out.println("Frame creato");
			JMain.setVisible(true);
			
			System.out.println(((Notify) arg).getFrame().getName());
			
		case OPEN_PAGE:
			/**
			 * case OPEN_PAGE
			 * Viene aperto un nuovo frame, ad esempio durante l'inserimento.
			 */
			break;
		
		default:
			break;
		}
		/*
		 * if(((Notify)arg).getErrorType().toString().compareTo(ErrorType.USER_FOUND
		 * .toString())==0){ JMain.setVisible(false); JMain=new
		 * SchermataPrincipale(Utente.getInstance().getUser());
		 * System.out.println("creata principale"); //
		 * 
		 * 
		 * // JMain.setVisible(true); }
		 */
	}

	public static synchronized PView getInstance() {
		if (Main == null)
			Main = new PView(null);
		return Main;
	}
}
